const host='http://api_devs.wanxikeji.cn'

const request = (url, options) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: `${host}${url}`,
      method: options.method,
      data: options.method === 'GET' ? options.data : JSON.stringify(options.data),
      header: {
        'Content-Type': 'application/json; charset=UTF-8',
        'x-token': 'x-token' // 看自己是否需要
      },
      success(request) {
        if(request.data.code==3000){
          wx.reLaunch({
            url:'/pages/login/login'
          })
        }
        if(request.data.code==3002){
          wx.reLaunch({
            url:'/pages/login/login'
          })
        }
        if (request.data.code === 2000) {
          
          resolve(request.data)
        } else {
          reject(request.data)
          wx.hideLoading({
            success: (res) => {},
          })
          wx.showToast({
            title: '网络卡顿，请稍后再试',
            icon: 'error',
            duration: 2000
          })
        }
      },
      fail(error) {
        wx.hideLoading({
          success: (res) => {

          },
        })
        wx.showToast({
          title: '网络卡顿，请稍后再试',
          icon: 'error',
          duration: 2000
        })
      }
    })
  })
}

const get = (url, options = {}) => {
  return request(url, {
    method: 'GET',
    data: options
  })
}

const post = (url, options) => {
  return request(url, {
    method: 'POST',
    data: options
  })
}

const put = (url, options) => {
  return request(url, {
    method: 'PUT',
    data: options
  })
}

// 不能声明DELETE（关键字）
const remove = (url, options) => {
  return request(url, {
    method: 'DELETE',
    data: options
  })
}

module.exports = {
  get,
  post,
  put,
  remove
}